/**
 * 根据value获取lable值
 * @param {Array<{label: String, value: Number | String}>} arr 数据源
 * @param {Number | String} value 值
 * @returns
 */
import { GetLabelByValueFromItem, ObjectAny } from '@/types/type-common'
import { ElMessage } from 'element-plus'

export const getLabelByValueFromArray = (arr: GetLabelByValueFromItem[], value: string | number, labelName = 'label', valueKey = 'value') => {
	const res = arr.filter(item => item[valueKey] === value)
	if (res.length) {
		return res[0][labelName]
	} else {
		return ''
	}
}

export function formatNumber(num: number) {
	return num >= 10 ? '' + num : '0' + num
}

export function dateFormat(time?: number | string, format = 'xxxx-xx-xx xx:xx:xx') {
	const date = time ? new Date(time) : new Date()
	const year = date.getFullYear()
	const month = formatNumber(date.getMonth() + 1)
	const day = formatNumber(date.getDate())
	const hours = formatNumber(date.getHours())
	const minutes = formatNumber(date.getMinutes())
	const second = formatNumber(date.getSeconds())

	if (format == 'xxxx-xx-xx xx:xx') {
		return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
	}
	if (format == 'xxxx-xx-xx') {
		return year + '-' + month + '-' + day
	}
	if (format == 'xx-xx') {
		return month + '-' + day
	}
	if (format == 'xxxx-xx-xx xx:xx:xx') {
		return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + second
	}
	if (format == 'xxxx年xx月xx日 xx:xx') {
		return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes
	}
	if (format == 'xxxx年xx月xx日 xx:xx:xx') {
		return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes + ':' + second
	}
	if (format == 'xxxx年xx月xx日 xx时') {
		return year + '年' + month + '月' + day + '日 ' + hours + '时'
	}
}

export function dateTimeFormat(time?: number | string, format = 'YYYY-MM-DD hh:mm:ss') {
	const date = time ? new Date(time) : new Date()
	const year = '' + date.getFullYear()
	const month = formatNumber(date.getMonth() + 1)
	const day = formatNumber(date.getDate())
	const hours = formatNumber(date.getHours())
	const minutes = formatNumber(date.getMinutes())
	const second = formatNumber(date.getSeconds())

	return format.replace('ss', second).replace('mm', minutes).replace('hh', hours).replace('DD', day).replace('MM', month).replace('YYYY', year)
}

export function formatDownTime(time: number) {
	const day = Math.floor(time / (1000 * 60 * 60 * 24))
	const hour = Math.floor((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
	const minute = Math.floor((time % (1000 * 60 * 60)) / (1000 * 60))
	const second = Math.floor((time % (1000 * 60)) / 1000)
	return {
		day: formatNumber(day > 0 ? day : 0),
		hour: formatNumber(hour > 0 ? hour : 0),
		minute: formatNumber(minute > 0 ? minute : 0),
		second: formatNumber(second > 0 ? second : 0)
	}
}

export function isEmpty(text: any): boolean {
	if (text === undefined || text === null || text === '') {
		return true
	}
	return false
}

export const cloneDeep = (obj: any) => {
	const objClone: any = Array.isArray(obj) ? [] : {}
	if (obj && typeof obj === 'object') {
		for (const key in obj) {
			if (obj[key] && typeof obj[key] === 'object') {
				objClone[key] = cloneDeep(obj[key])
			} else {
				objClone[key] = obj[key]
			}
		}
	}
	return objClone
}

export const copyText = (text: string) => {
	try {
		navigator.clipboard.writeText(text)
	} catch (error) {
		const textarea = document.createElement('textarea')
		textarea.value = text
		document.body.appendChild(textarea)
		textarea.select()
		document.execCommand('copy')
		document.body.removeChild(textarea)
	}
}

export const getAssetURL = (path: string) => {
	// 参数一: 相对路径  参数二: 当前路径的URL
	return new URL(`../assets/${path}`, import.meta.url).href
}

export const formatGetParams = (params: ObjectAny) => {
	let str = ''
	for (const key in params) {
		if (Object.prototype.hasOwnProperty.call(params, key)) {
			const ele = params[key]
			if (ele) {
				str += `${key}=${ele}&`
			}
		}
	}

	return str
}

export const commonDownLoad = (url: string, filename?: string) => {
	const eLink = document.createElement('a')
	eLink.download = filename || '附件'
	eLink.style.display = 'none'
	eLink.href = url
	document.body.appendChild(eLink)
	eLink.click()
	document.body.removeChild(eLink)
}

export const imgDownload = (url: string, filename?: string) => {
	fetch(url, {
		headers: {
			Origin: '*'
		}
	})
		.then(async res => await res.blob())
		.then(blob => {
			const a = document.createElement('a')
			a.style.display = 'none'
			a.download = filename || '附件'
			a.href = URL.createObjectURL(blob)
			document.body.appendChild(a)
			a.click()
			document.body.removeChild(a)
		})
}
